package com.ruoyi.web.controller.tool;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.framework.web.service.DictService;
import com.ruoyi.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SiteService {

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private DictService dictService;

    /**
     * 设置站点
     * @param site
     */
    public void setSiteId(Long site)
    {
        HttpServletRequest request = ServletUtils.getRequest();
        HttpSession session = request.getSession();
        session.setAttribute("admin_site_id",site);
    }

    /**
     * 获取站点
     * @return
     */
    public Long getSiteId()
    {
        HttpServletRequest request = ServletUtils.getRequest();
        HttpSession session = request.getSession();
        Object site_id = session.getAttribute("admin_site_id");
        if (site_id != null) {
            return Long.parseLong(site_id.toString());
        }

        SysUser user = ShiroUtils.getSysUser();
        List<SysRole> roles = user.getRoles();
        List<Long> roleIds = roles.stream().map(SysRole::getRoleId).collect(Collectors.toList());

        Long siteId = null;
        if (!roleIds.contains(1L)) {
            for (Long roleId : roleIds) {
                List<Long> longs = sysRoleService.selectRoleSite(roleId);
                siteId = longs.get(0);
                break;
            }
        } else {
            List<SysDictData> sys_site_type = dictService.getType("sys_site_type");
            for (SysDictData sysDictData : sys_site_type) {
                siteId = Long.parseLong(sysDictData.getDictValue());
                break;
            }
        }
        session.setAttribute("admin_site_id",siteId);
        return siteId;
    }


    public List<Map<String,Object>> getSiteList()
    {
        List<Map<String,Object>> objects = new ArrayList<>();
        List<Long> objects1 = new ArrayList<>();

        SysUser user = ShiroUtils.getSysUser();
        List<SysRole> roles = user.getRoles();
        List<Long> roleIds = roles.stream().map(SysRole::getRoleId).collect(Collectors.toList());

        if (!roleIds.contains(1L)) {
            for (Long roleId : roleIds) {
                List<Long> longs = sysRoleService.selectRoleSite(roleId);
                for (Long site : longs) {
                    if (!objects1.contains(site)) {
                        String label = dictService.getLabel("sys_site_type", site.toString());
                        HashMap<String, Object> hashMap = new HashMap<>();
                        hashMap.put("label",label);
                        hashMap.put("value",site);
                        objects.add(hashMap);
                        objects1.add(site);
                    }
                }
            }
        } else {
            List<SysDictData> sys_site_type = dictService.getType("sys_site_type");
            for (SysDictData sysDictData : sys_site_type) {
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("label",sysDictData.getDictLabel());
                hashMap.put("value",sysDictData.getDictValue());
                objects.add(hashMap);
            }
        }

        return objects;
    }


}
